﻿using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Threading.Tasks;

namespace WebApiExtend.Middleware
{
    /// <summary>
    /// 日志管道
    /// </summary>
    public class HeaderMiddleware : BaseMiddleware
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="next"></param>
        public HeaderMiddleware(RequestDelegate next) : base(next)
        {
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext httpContext)
        {
            /*
             说明: 
                ltctraid    整个链路唯一id
                requestid   我自己唯一id
                requestpid  父级id
             */
            //if (string.IsNullOrEmpty(httpContext.Request.Headers["x-mg-ltctraid"].ToString()))
            //{
            //    httpContext.Request.Headers["x-mg-ltctraid"] = Guid.NewGuid().ToString("N");
            //}

            //if (string.IsNullOrEmpty(httpContext.Request.Headers["x-mg-requestpid"].ToString()))
            //{
            //    httpContext.Request.Headers["x-mg-requestpid"] = Guid.NewGuid().ToString("N");
            //}

            //if (string.IsNullOrEmpty(httpContext.Request.Headers["x-mg-requestid"].ToString()))
            //{
            //    httpContext.Request.Headers["x-mg-requestid"] = Guid.NewGuid().ToString("N");
            //}
            await base._next.Invoke(httpContext);
        }
    }
}
